// 获取用户ID 
let user = localStorage.getItem("phonenum");
// 获取空购物车
let nocart = document.querySelector(".cat_center");
// 获取有商品的购物车
let yescart = document.querySelector(".cart_products");
ajax({
	url: "../html/productscart.php",
	dataType: "json",
	data: {
		user
	}
}).then(res => {
	res.forEach(item => {
		item.check = true;
	})
	renderDom(res);
	countPrice(res);
	if (res == "" || res == null) {
		nocart.style.display = "block";
		yescart.style.display = "none";
	}
})
// 渲染函数
function renderDom(res) {
	let p_list = document.querySelector(".p_list");
	let g_list = document.querySelector(".goodlist");
	g_list.innerHTML = "";
	res.forEach(item => {
		g_list.innerHTML += `
		<li>
		    <input type="checkbox" class="p_check"/>
			<div class="p_img">
				<img src="${item.img}" >
			</div>
			<p class="p_name">${item.productsname}</p>
			<p class="p_price">￥${item.price}</p>
			<div class="p_count">
			    <a class="p_min" style="cursor: pointer;" cid="${item.id}">-</a>
			    <input type="text" value="${item.productsnum}"/ class="p_num" readonly="readonly">
			    <a class="p_add" style="cursor: pointer;" cid="${item.id}">+</a>
			</div>
			<p class="p_priceall">￥${item.price*item.productsnum}</p>
			<a class="p_del" style="cursor: pointer;">删除</a>
		</li>
		`
		midpint(res);
		delE(res);
		countP(res);
	})
	CheckALL(res);
}

function CheckALL(res) {
	// console.log(res)
	//获取全选框
	let checkAllEle = document.querySelector("#checkall");
	//所有单选框
	let checkEle = document.querySelectorAll(".p_check");
	checkAllEle.onclick = function() {
		//全选全不选
		checkEle.forEach(item => {
			item.checked = this.checked;
		})
		res.forEach(items => {
			items.check = !items.check;
		})
		countPrice(res);
	}
	checkEle.forEach((it, key) => {
		it.onclick = function() {
			checkAllEle.checked = isAllChecked();
			res[key].check = !res[key].check;
			it.check = !it.check;
			console.log(it);
			if (it.checked == false) {
				checkAllEle.checked = false;
			}
			countPrice(res);
		}
	})
	//判断是否为全选
	function isAllChecked() {
		for (var j = 0; j < checkEle.length; j++) {
			if (!checkEle[j].checked)
				return false;
		}
		return true;
	}
}
//事件委托
function midpint(data) {
	let countEle = document.querySelectorAll(".p_count");
	[...countEle].forEach((item, key) => {
		let cid = data[key].id;
		item.onclick = function(e) {
			let event = e || window.event;
			var target = event.target;
			if (target.innerText === "+") {
				ajax({
					url: "../html/addnum.php",
					dataType:"json",
					data: {
						cid
					}
				}).then(res => {
					let inpVal = this.firstElementChild.nextElementSibling;
					if (res.status == "1") {
						inpVal.value++;
						data[key].productsnum++;
						countPrice(data);
						countP(data);
					}
				})
			} else if (target.innerText === "-") {
				ajax({
					url: "../html/minnum.php",
					dataType: "json",
					data: {
						cid
					}
				}).then(res => {
					let inpVal = this.firstElementChild.nextElementSibling;
					if (res.status == "1") {
						if (inpVal.value > 1) {
							inpVal.value--;
							data[key].productsnum--;
							countPrice(data);
							countP(data);
						} else {
							inpVal.value = 1;
							alert("商品数量最小为1!");
						}
					}
				})
			}
		}
	})
}
// 删除功能
function delE(data){
	let delEle = document.querySelectorAll(".p_del");
	delEle.forEach((delEle,key)=>{
		delEle.onclick = function(){
			let r = confirm("您确定需要删除此商品吗?")
			if(r == true){
			let cid = data[key].id;
			ajax({
				url:"../html/delcart.php",
				dataType:"json",
				data:{
				    cid
				}
			}).then(res=>{
				if(res.status == "1"){
				ajax({
					url:"../html/productscart.php",
					dataType:"json",
					data:{
						user
					}
				}).then(res=>{
					if(res=="" || res == null){
						nocart.style.display = "block";
						yescart.style.display = "none";
					}
					res.forEach(item=>{
						item.check = true;
					})
					renderDom(res);
					countPrice(res);
					CheckALL(res);
				})
				}
			})	
			}else{
				return
			}
		}
	})
}
// 计算小计
function countP(res){
	$(".p_add").click(function(){
		var n = $(this).siblings(".p_num").val();
		n = parseInt(n)+1;
		var p = $(this).parent().siblings(".p_price").html();
		p = p.substr(1);
		$(this).parent().siblings(".p_priceall").html("￥"+p * n);
	});
	$(".p_min").click(function(){
		var n = $(this).siblings(".p_num").val();
		n = parseInt(n)-1;
		if(n<1){
			n = 1;
		}
		var p = $(this).parent().siblings(".p_price").html();
		p = p.substr(1);
		$(this).parent().siblings(".p_priceall").html("￥"+p * n);
	})
}; 
//计算总价格
function countPrice(res) {
	let pall = 0;
	let pallEle = document.querySelector(".price_all");
	if (res == "" || res == null) {
		pallEle.innerText == "0"
	}
	res.forEach(item => {
		if (!item.check) {
			pall += item.price * item.productsnum;
		}
	})
	pallEle.innerText = pall;
}
